package ar.uade.fain.ai.tpo.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import java.util.Set;

import ar.uade.fain.ai.tpo.model.Caracteristica;
import ar.uade.fain.ai.tpo.model.Categoria;

public class CaracteristicaDAO extends CacheDAOSupport<Caracteristica> {


	private static final String INSERT_CARACTERISTICA =
			"INSERT INTO DELIVERY.DBO.CARACTERISTICA " +
			"(idcaracteristica, descripcion, tiempoCoccion,complejidad,activo) " +
			"VALUES (?,?,?,?,1)";
	
	private static final String GET_BY_ID =
			"SELECT * FROM DELIVERY.DBO.CARACTERISTICA " +
			"WHERE IDCARACTERISTICA = ? AND ACTIVO = 1";
	
	private static final String GET_NEXT_ID = 
			"SELECT ISNULL(MAX(IDCARACTERISTICA),0)+1 AS ID " +
			"FROM DELIVERY.DBO.CARACTERISTICA";
	
	public CaracteristicaDAO() {
		super();
	}
	
	public Caracteristica buscarPorId(final Long id) {
		final Set<Caracteristica> enCache = buscarEnCache(
				new Selector<Caracteristica>() {
			@Override
			public boolean aceptar(Caracteristica entidad) {
				return entidad.getId().equals(id);
			}
		});
		if (enCache.isEmpty()) {
			return load(id);
		} else {
			return enCache.iterator().next();
		}
	}

	private Caracteristica load(final Long id) {
		final List<Caracteristica> results = database().select(GET_BY_ID, 
			id, new RowMapper<Caracteristica>() {
			@Override
			public Caracteristica map(ResultSet rs) throws SQLException {
				final Caracteristica caracteristica = new Caracteristica();
				caracteristica.setId(rs.getLong("idcaracteristica")); 
				caracteristica.setDescripcion(rs.getString("descripcion")); 
				caracteristica.setTiempoCoccion(rs.getDouble("tiempococcion")); 
				caracteristica.setAlHorno(rs.getBoolean("alHorno"));
				caracteristica.setComplejidad(rs.getInt("complejidad"));
				CategoriaDAO cDAO = new CategoriaDAO();
				Categoria categoria = cDAO.buscarPorId(rs.getLong("categoria"));
				caracteristica.setCategoria(categoria);
						
				return caracteristica;
			}
		});
		final Caracteristica result = results.isEmpty() ? null : results.get(0);
		agregarACache(result);
		return result;
	}
	
	public void guardar(final Caracteristica caracteristica) {
		final Long nextId = database().selectNextId(GET_NEXT_ID);
		database().insert(INSERT_CARACTERISTICA, new ParameterSetter() {
			@Override
			public void setParametros(final PreparedStatement statement) 
					throws SQLException {
				statement.setLong(1, nextId);
				statement.setString(2, caracteristica.getDescripcion());
				Double tiempoCoccion = caracteristica.getTiempoCoccion();
				if (tiempoCoccion == null) {
					statement.setNull(3, Types.DOUBLE);
				} else {
					statement.setDouble(3, tiempoCoccion);
				}
				Integer complejidad = caracteristica.getComplejidad();
				if (complejidad == null) {
					statement.setNull(4, Types.INTEGER);
				} else { 
					statement.setInt(4, complejidad);
				}
			}
		});
		caracteristica.setId(nextId);
		agregarACache(caracteristica);
	}
	
}
